// Copyright (c) 2011 Alexander Gnodtke. All rights reserved.

// This file is part of MAD (Multi Address Display) browser extension.

// MAD (Multi Address Display) is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// MAD (Multi Address Display) is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with MAD (Multi Address Display).  If not, see <http://www.gnu.org/licenses/>.


// Location class
function Location(gMarker) {
    this.gMarker = gMarker;
}

Location.prototype = {
    // DOES NOT WORK myProperty: ”sthis is a string”,
    myMethod: function(parms){
        //method logic here
    }   
}



// Simple print function for i18n
function p(key) {
    document.write(chrome.i18n.getMessage(key));
}

function initialize(locations) {	
    var latlng = new google.maps.LatLng(0, 0);
    var options = {
        zoom: 5,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("map_area"), options);


    var latLngBounds = new google.maps.LatLngBounds();

    for (i = 0; i< locations.length;i = i+1) {				
        locations[i].gMarker.setMap(map);
        latLngBounds.extend(locations[i].gMarker.position);
    }

    map.setCenter(latLngBounds.getCenter());
    if ( locations.length<2) {
        map.setZoom(9);
    } else {
        map.fitBounds(latLngBounds);	 
    }
}

// Method fills the diffs with the location's
// address and marker
function writeLocations(locations) {
    var list = document.getElementById('address_list');	
    var cnt = 0;
    for (i=0; i<locations.length;i=i+1) {
        var newA = document.createElement('div');
	    var newAid = 'address'+cnt;
		newA.id = newAid; 
        var newAImg = document.createElement('img');
		newAImg.setAttribute('src',locations[i].gMarker.icon); 
        var newAText = document.createElement('label');
        newAText.innerHTML = "&nbsp;"+locations[i].gMarker.title+"&nbsp;";  
        newA.appendChild(newAImg);
		newA.appendChild(newAText);
		list.appendChild(newA);
		cnt++;
    }
}

function getMarkerIcon(position) {
    var c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(position);
    return "gfx/red_Marker"+c+".png";
}

function setAddresses(addArr) {
    var locations = [];
    if (addArr && addArr.length>0) { 
        for (i=0; i<addArr.length;i = i+1) {
	        var latlng = new google.maps.LatLng(addArr[i][1],addArr[i][2]);			
	        var marker = new google.maps.Marker({
		        position: latlng,
		        title:addArr[i][0],
                icon: getMarkerIcon(i)
	        });				
            var loc = new Location(marker);
            locations[locations.length] = loc;
        }
    }
    initialize(locations);
    writeLocations(locations);
}

// initialize map page
document.title = chrome.i18n.getMessage("map_html_title");
// get Addresses
chrome.extension.sendRequest({'action' : 'getAddresses'},setAddresses);

